﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using QuanLyKhachSanDAO.DTO;
using System.Data;

namespace QuanLyKhachSanDAO.DAO
{
    public class PhongDAO
    {
        public PhongDTO[] getDsPhong()
        {
            string query = "SELECT * FROM Phong";

            PhongDTO[] arr;
            DataProvider dp = new DataProvider();

            DataTable dt = dp.ExecuteQuery(query);
            int i, n = dt.Rows.Count;
            arr = new PhongDTO[n];
            for (i = 0; i < n; i++)
            {
                object hs = GetDataFromDataRow(dt, i);
                arr[i] = (PhongDTO)hs;
            }
            return arr;
        }

        protected object GetDataFromDataRow(DataTable dt, int i)
        {
            PhongDTO ph = new PhongDTO();
            ph.MaPhong = int.Parse(dt.Rows[i]["MaPhong"].ToString());
            ph.MaLoaiPhong = int.Parse(dt.Rows[i]["MaLoaiPhong"].ToString());
            ph.TenPhong = dt.Rows[i]["TenPhong"].ToString();
            ph.GhiChu = dt.Rows[i]["GhiChu"].ToString();
            ph.HienTrangPhong = int.Parse(dt.Rows[i]["HienTrangPhong"].ToString());
            ph.TinhTrang = bool.Parse(dt.Rows[i]["TinhTrang"].ToString());
            return (object)ph;
        }
        //-----------Thêm phòng mới----------------
        public bool LapDanhMucPhong(PhongDTO ph)
        {
            bool kq = false;
            int bit = 0;
            if (ph.TinhTrang == true)
                bit = 1;
            string sql = string.Format("insert into Phong(MaLoaiPhong,TenPhong,GhiChu,HienTrangPhong,TinhTrang) values({0},N'{1}',N'{2}',{3},{4})",ph.MaLoaiPhong,ph.TenPhong,ph.GhiChu,ph.HienTrangPhong,bit);
            DataProvider dt = new DataProvider();
            kq = dt.ExecuteNonQuery(sql);
            return kq;
        }
        //---------Lấy Danh Sách Phòng theo loại phòng--------------
        public  PhongDTO[] LayDanhSachPhongTheoMaLoai(int maLoai)
        {
            PhongDTO[] arr;
            DataTable dt = new DataTable();
            string sql = "select * from Phong p where p.maloaiphong=" + maLoai;
            DataProvider dp = new DataProvider();
            dt = dp.ExecuteQuery(sql);
            int  n = dt.Rows.Count; // số lượng phần tử mảng
            arr = new PhongDTO[n+1];
            PhongDTO p1 = new PhongDTO();
            p1.MaPhong = -1;
            p1.MaLoaiPhong = -1;
            p1.TenPhong = "---Chọn Phòng---";
            p1.TinhTrang = true;
            p1.HienTrangPhong = 1;
            p1.GhiChu = "aa";
            arr[0] = p1;
            for (int i = 0; i < n; i++)
            {
                object ph = GetDataFromDataRow(dt, i);
                arr[i + 1] = (PhongDTO)ph;
            }
            return arr;
        }
    }
}